/**
 * 秒杀模块
 */
var seckill = {
	URL : {
		now : "http://127.0.0.1/time/now",
		//暴露秒杀地址
		expose : function (seckillId) {
			return "http://127.0.0.1/seckill/"+seckillId+"/expose";
		} ,
		execute : function (seckillId) {
			return "http://127.0.0.1/seckill/"+seckillId+"/execute";
		}
	},
	detail : {
		/**
		 * id:秒杀ID, startTime:秒杀开始时间, endTime:秒杀结束时间
		 */
		init : function(params) {
			var seckillId = params.id;
			var startTime = params.startTime;
			var endTime = params.endTime;
			
			// 获取系统当前时间
			$.get(seckill.URL.now, {}, function(result) {
				if (result && result['success']) {
					var nowTime = result['data'];
					seckill.detail.countDown(seckillId, startTime, endTime, nowTime);
				}
			});
		},
		
		/**
		 * 提示
		 */
		message : function(message) {
			$('#countdown_box').html(message);
		},
	
		execute: function (seckillId,secretKey,userid) {
			$.post(seckill.URL.execute(seckillId), {secretKey:secretKey,userid:userid}, function(result){
				if (result && result['success']) {
					var data = result["data"];
					if (data && data['success']) {
						//秒杀成功
						seckill.detail.message(data['message']);
					} else {
						seckill.detail.message(data['message']);
					}
				} else {
					console.log("result:"+result);
				}
			});
		},
		login: function(username,password) {
			//验证用户名密码，正确后在cookie保存用户ID
			if (username && password && username=="hugo" && password=="123456") {
				var userid = 12345678; //模拟生成userid
				$.cookie('userid', userid, { path: "/", expires: 1 });
				return true;
			} else {
				return false;
			}
		},
		/**
		 * 处理秒杀业务逻辑
		 */
		handleSeckill : function(seckillId,node) {
			//暴露秒杀接口
			if (seckillId && seckillId > 0) {
				$.get(seckill.URL.expose(seckillId), {}, function(result) {
					if (result && result['success']) {
						var exposer = result["data"];
						if (exposer["exposed"]) {
							//开启秒杀
							var secretKey = exposer["secretKey"];
							var $node = $(node);
							$node.hide()
								.html('<button id="killBtn" data-target="#loginDialog" class="btn btn-primary">秒杀</button>')
								.show();
							var seckillId = exposer["seckillId"];
							$(this).removeClass('disabled').addClass('enabled'); //打开按钮
							$('#killBtn').one("click",{seckillId:seckillId,secretKey:secretKey}, seckill.detail.killButtonClick);
						} else {
							//未开启秒杀，重新计时
							var seckillId = exposer["seckillId"];
							var startTime = exposer["startTime"];
							var endTime = exposer["endTime"];
							var timeNow = exposer["timeNow"];
							seckill.detail.countDown(seckillId, startTime, endTime, timeNow);
						}
					}else {
						console.log("result:"+result);
					}
				});
			}
		},
		//秒杀按钮的点击函数
		killButtonClick: function(event) {
			//关闭按钮
			$(this).removeClass('enabled').addClass('disabled');
			
			var seckillId = event.data.seckillId;
			var secretKey = event.data.secretKey;
			
			//判断是否登录
			var userid = $.cookie('userid');
			if (userid) {
				seckill.detail.execute(seckillId,secretKey,userid);
			} else {
				var $loginDialog = $('#loginDialog');
				$loginDialog.modal({
					keyboard: false,
					backdrop: "static"
				});
				var $loginMessage = $("#loginMessage");
				$loginMessage.hide(); //隐藏
				$('#loginBtn').one("click",function(){
					//获取表单数据，登录
					var username = $('#username').val();
					var password = $('#password').val();
					var success = seckill.detail.login(username,password);
					if (success) {
						//登录成功，重载页面
						window.location.reload();
					}else {
						//登录失败，显示提示信息
						$loginMessage.html('登录失败，请使用hugo/123456').show();
						//重新绑定事件
						$('#killBtn').one("click",{seckillId:seckillId,secretKey:secretKey}, seckill.killButtonClick);
					}
				});
				$('#closeBtn').one("click",function(){
					//重新打开秒杀开关
					$('#killBtn').removeClass('disabled').addClass('enabled'); //打开按钮
				});
			}
		},
		
		/**
		 * 秒杀倒计时
		 */
		countDown : function(seckillId, startTime, endTime, nowTime) {
			
			var $countdownBox = $('#countdown_box');
			if (nowTime < startTime) { //还没开始秒杀
				//开始倒计时
				var killTime = startTime + 200;
				
				$countdownBox.countdown(killTime, function(event) {
				    $(this).html(event.strftime('秒杀时间：%D天 %H时 %M分 %S秒'));
				}).on('finish.countdown', function(event) {
					//倒计时结束,处理秒杀业务逻辑
					seckill.detail.handleSeckill(seckillId, $countdownBox.get(0));
				});
			} else if (nowTime >= endTime) {// 秒杀结束
				//删除元素，添加秒杀结束提示
				$countdownBox.html("秒杀结束");
			} else { //秒杀进行时
				//处理秒杀业务逻辑
				seckill.detail.handleSeckill(seckillId, $countdownBox.get(0));
			}
			
		},
	}
}